package metu.tespit.session.course;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import metu.tespit.entitiy.accountManagement.User;
import metu.tespit.entity.course.ProjectDocument;
import metu.tespit.entity.course.ProjectTeam;
import metu.tespit.entity.course.ReviewPair;
import metu.tespit.session.schedule.ActivityHome;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.Identity;

/**
 
 * @created Jun 9, 2012
 */
@Name("userSubmissionLists")
public class UserSubmissionLists {
	
	@In
	EntityManager entityManager;
	
	@In
	Identity identity;
	
	@In
	ActivityHome activityHome;
	
	@SuppressWarnings("unchecked")
	public List<ProjectDocument> teamsSubmissons(Long a){
		User us=(User)entityManager.createQuery("Select u from User u where u.username=:usnam").setParameter("usnam", identity.getCredentials().getUsername()).getSingleResult();
		ProjectTeam pt = us.getProjectTeam();
		if(pt==null){
			return new ArrayList<ProjectDocument>();
		}
		
		List<ProjectDocument> projectDm=(List<ProjectDocument>)entityManager.createQuery("select p from ProjectDocument p where p.uploaderActivity.id =:uploaderActivity and p.projectTeam =:pt")
		.setParameter("uploaderActivity", a).setParameter("pt", pt).getResultList();
		return projectDm;
	}
	
	@SuppressWarnings("unchecked")
	public List<ProjectDocument> reviewSubmissions(Long a){
		User us=(User)entityManager.createQuery("Select u from User u where u.username=:usnam").setParameter("usnam", identity.getCredentials().getUsername()).getSingleResult();
		ProjectTeam pt = us.getProjectTeam();
		if(pt==null){
			return new ArrayList<ProjectDocument>();
		}
		ProjectTeam toRev=null;
		for(ReviewPair search : activityHome.getInstance().getReviewPairs()){
			if(search.getReviewer()==pt){
				toRev=search.getReviewee();
				break;
			}
		}
		if(toRev == null)
			return new ArrayList<ProjectDocument>();
		
		List<ProjectDocument> projectDm=(List<ProjectDocument>)entityManager.createQuery("select p from ProjectDocument p where p.uploaderActivity.id =:uploaderActivity and p.projectTeam =:pt order by p.id asc")
		.setParameter("uploaderActivity", activityHome.getInstance().getReviewActivity().getId()).setParameter("pt", toRev).getResultList();
		if(projectDm.size()<2){
			return projectDm;
		}
		return projectDm.subList(projectDm.size()-1, projectDm.size());
	}

}
